import Vue from 'vue';
import Router from 'vue-router';
const Login=() => import(/* webpackChunkName: "public-group" */ '@/pages/Login');
const Layout=() => import(/* webpackChunkName: "public-group" */ '@/pages/Layout');
// 菜单管理
const Menu=() => import(/* webpackChunkName: "menu-group" */ '@/pages/menu/Index');
const MenuAdd=() => import(/* webpackChunkName: "menu-group" */ '@/pages/menu/Add');
const MenuEdit=() => import(/* webpackChunkName: "menu-group" */ '@/pages/menu/Edit');
// 角色管理
const Role=() => import(/* webpackChunkName: "role-group" */ '@/pages/role/Index');
const RoleAdd=() => import(/* webpackChunkName: "role-group" */ '@/pages/role/Add');
const RoleEdit=() => import(/* webpackChunkName: "role-group" */ '@/pages/role/Edit');
// 管理员
const Admin=() => import(/* webpackChunkName: "admin-group" */ '@/pages/admin/Index');
const AdminAdd=() => import(/* webpackChunkName: "admin-group" */ '@/pages/admin/Add');
const AdminEdit=() => import(/* webpackChunkName: "admin-group" */ '@/pages/admin/Edit');
// 分类管理
const Category=() => import(/* webpackChunkName: "category-group" */ '@/pages/category/Index');
const CategoryAdd=() => import(/* webpackChunkName: "category-group" */ '@/pages/category/Add');
const CategoryEdit=() => import(/* webpackChunkName: "category-group" */ '@/pages/category/Edit');
// 规格管理
const Specs=() => import(/* webpackChunkName: "specs-group" */ '@/pages/specs/Index');
const SpecsAdd=() => import(/* webpackChunkName: "specs-group" */ '@/pages/specs/Add');
const SpecsEdit=() => import(/* webpackChunkName: "specs-group" */ '@/pages/specs/Edit');
// 商品管理
const Goods=() => import(/* webpackChunkName: "goods-group" */ '@/pages/goods/Index');
const GoodsAdd=() => import(/* webpackChunkName: "goods-group" */ '@/pages/goods/Add');
const GoodsEdit=() => import(/* webpackChunkName: "goods-group" */ '@/pages/goods/Edit');
const RichText=() => import(/* webpackChunkName: "goods-group" */ '@/pages/goods/RichText');


// echarts图表库
const Bar=() => import(/* webpackChunkName: "charts-group" */ '@/pages/charts/Bar');
const Pie=() => import(/* webpackChunkName: "charts-group" */ '@/pages/charts/Pie');
const Line=() => import(/* webpackChunkName: "charts-group" */ '@/pages/charts/Line');
const Home=() => import(/* webpackChunkName: "charts-group" */ '@/pages/Home');

Vue.use(Router);

const router = new Router({
  routes: [
    {
      path: '/',
      component: Layout,
      // 路由重定向
      redirect:'/home',
      // 二级路由
      children:[
        {
          path:'/home',
          component:Home,
          meta:{
            title:'销售业绩统计'
          }
        },
        {
          path:'/menu',
          component:Menu,
          meta:{
            title:'菜单列表'
          }
        },
        {
          path:'/menu/add',
          component:MenuAdd,
          meta:{
            title:'菜单添加'
          }
        },
        {
          path:'/menu/:id',
          component:MenuEdit,
          meta:{
            title:'菜单编辑'
          }
        },
        {
          path:'/role',
          component:Role,
          meta:{
            title:'角色列表'
          }
        },
        // /role/add
        // 路由规则书写顺序: 动态路由写在最后边
        {
          path:'/role/add',
          component:RoleAdd,
          meta:{
            title:'角色添加'
          }
        },
        {
          path:'/role/:id',
          component:RoleEdit,
          meta:{
            title:'角色编辑'
          }
        },
        {
          path:'/admin',
          component:Admin,
          meta:{
            title:'管理员列表'
          }
        },
        {
          path:'/admin/add',
          component:AdminAdd,
          meta:{
            title:'管理员添加'
          }
        },
        {
          path:'/admin/:id',
          component:AdminEdit,
          meta:{
            title:'管理员编辑'
          }
        },
        {
          path:'/category',
          component:Category,
          meta:{
            title:'分类列表'
          }
        },
        {
          path:'/category/add',
          component:CategoryAdd,
          meta:{
            title:'分类添加'
          }
        },
        {
          path:'/category/:id',
          component:CategoryEdit,
          meta:{
            title:'分类编辑'
          }
        },
        {
          path:'/specs',
          component:Specs,
          meta:{
            title:'规格列表'
          }
        },
        {
          path:'/specs/add',
          component:SpecsAdd,
          meta:{
            title:'规格添加'
          }
        },
        {
          path:'/specs/:id',
          component:SpecsEdit,
          meta:{
            title:'规格编辑'
          }
        },
        {
          path:'/goods',
          component:Goods,
          meta:{
            title:'商品列表'
          }
        },
        {
          path:'/goods/add',
          component:GoodsAdd,
          meta:{
            title:'商品添加'
          }
        },
        {
          path:'/goods/:id',
          component:GoodsEdit,
          meta:{
            title:'商品编辑'
          }
        },
        {
          path:'/richtext',
          component:RichText
        },
        {
          path:'/bar',
          component:Bar,
          meta:{
            title:'柱状图演示'
          }
        },
        {
          path:'/pie',
          component:Pie,
          meta:{
            title:'饼状图演示'
          }
        },
        {
          path:'/line',
          component:Line,
          meta:{
            title:'折线图演示'
          }
        }
      ]
    },
    {
      path: '/login',
      name: 'Login',
      component: Login,
      meta:{
        title:'管理员登录'
      }
    },
    // 404路由
    {
      path:'**',
      redirect:'/home'
    }
  ]
});
// 添加一个全局的路由导航守卫: 登录拦截
router.beforeEach((to, from, next) => {
  // 动态设置页面标题
  document.title=to.meta.title;
  // 如果to.path!='/loign'; 
  if(to.path!='/login'){
      // 判断登录状态  JSON.parse('{}')  null
      const userInfo=JSON.parse(sessionStorage.getItem('userinfo')||'{}');
      //没有登录
      if(!userInfo.token){
         // 重定向到login
        return next('/login');
      }
      // 登录状态
      return next();
  }
  next();
});

export default router;
